/**
 *
 */
package com.dhcc.android.base.utils.dataparse;

import android.util.Xml;

import com.dhcc.android.base.common.bean.BasicProperties;
import com.dhcc.android.base.common.bean.Item;

import org.xmlpull.v1.XmlPullParser;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


/**
 * thc文件的解析公共方法
 *
 * @author pengling
 * @date 2015-6-1
 */
public class ThcParse {
    /**
     * 解析XML
     *
     * @return 返回Object
     */
    public static List<BasicProperties> getXmlThc(InputStream is) {
        PullParse pullParse = new PullParse();
        XmlPullParser parser = Xml.newPullParser();
        List<BasicProperties> propertyList = null;
        BasicProperties basicProperties = null;
        List<Item> itemList = null;
        try {
            parser.setInput(is, "UTF-8");
            // 事件类型
            int eventType = parser.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) {
                switch (eventType) {
                    case XmlPullParser.START_DOCUMENT:
                        propertyList = new ArrayList<BasicProperties>();
                        break;
                    case XmlPullParser.START_TAG:
                        // 获得当前节点元素的名称
                        String nodeName = parser.getName();
                        if ("FIELD".equalsIgnoreCase(nodeName)) {
                            basicProperties = new BasicProperties();
                            itemList = new ArrayList<Item>();
                            pullParse.setChildParam(parser, basicProperties);
                        } else if ("ITEM".equalsIgnoreCase(nodeName)) {
                            Item item = new Item();
                            pullParse.setChildParam(parser, item);
                            if (itemList != null) {
                                itemList.add(item);
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if ("FIELD".equalsIgnoreCase(parser.getName())) {
                            basicProperties.setItem(itemList);
                            propertyList.add(basicProperties);
                        }
                }
                eventType = parser.next();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return propertyList;
    }
}
